import { ref } from "vue";

const Params = ref(null);

function getCurrentHashPathAndQuery(
  hashPath = window.location.hash,
  isAuth = false,
) {
  let queryString = hashPath.includes("?") ? hashPath.split("?")[1] : "";
  if (hashPath.startsWith("#/code=")) {
    return getCurrentHashPathAndQuery(hashPath.replace("#/", "?"), true);
  }

  return {
    hashPath: hashPath,
    queryString: queryString,
    isAuth: isAuth,
  };
}

// 解析查询参数
function getAllQueryParams() {
  let { queryString, isAuth, hashPath } = getCurrentHashPathAndQuery();
  let params = {};

  if (queryString) {
    let pairs = queryString.split("&");
    pairs.forEach((pair) => {
      let [key, value] = pair.split("=");
      params[key] = decodeURIComponent(value || "");
    });

    if (isAuth) {
      params.isAuth = true;
      params.AuthString = hashPath.replace("?", "#");
    }
    return params;
  }

  return null;
}

// 序列化查询参数
function serialize(obj) {
  let str = [];
  for (let p in obj) {
    if (p === "createTime" || p === "") continue;
    if (obj.hasOwnProperty(p)) {
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
    }
  }
  return str.join("&");
}

// 添加或更新查询参数
function addOrUpdateQueryParam(key, value) {
  let { hashPath, queryString } = getCurrentHashPathAndQuery();
  let queryParams = getAllQueryParams() ?? {};

  // 更新或添加新的查询参数
  queryParams[key] = value;

  // 构建新的查询参数部分
  let newQueryString = serialize(queryParams);

  // 构建新的 URL，包括 hash 路径和查询参数部分
  let newHashPath = hashPath.split("?")[0]; // 移除旧的查询参数
  let newUrl = newHashPath + (newQueryString ? "?" + newQueryString : "");

  // 使用 history.replaceState 更新 URL
  window.history.replaceState({}, "", newUrl);
}

function addAuthCode(authString) {
  console.log(window.location.href + authString);
  window.history.replaceState({}, "", window.location.href + authString);
}

// 删除指定查询参数
function deleteQueryParam(key) {
  let { hashPath, queryString } = getCurrentHashPathAndQuery();
  let queryParams = getAllQueryParams();

  try {
    // 删除指定的查询参数
    delete queryParams[key];

    // 构建新的查询参数部分
    let newQueryString = serialize(queryParams);

    // 构建新的 URL，包括 hash 路径和查询参数部分
    let newHashPath = hashPath.split("?")[0]; // 移除旧的查询参数
    let newUrl = newHashPath + (newQueryString ? "?" + newQueryString : "");

    // 使用 history.replaceState 更新 URL
    window.history.replaceState({}, "", newUrl);
  } catch {}
}

// 清除所有查询参数
function clearQueryParams() {
  let { hashPath } = getCurrentHashPathAndQuery();

  // 构建新的 URL，只保留 hash 路径部分
  let newUrl = hashPath.split("?")[0];

  // 使用 history.replaceState 更新 URL
  window.history.replaceState({}, "", newUrl);
}

// 获取指定查询参数的值
function getQueryParam(key) {
  let queryParams = getAllQueryParams();
  try {
    return queryParams[key];
  } catch {
    return null;
  }
}

export default {
  getCurrentHashPathAndQuery,
  addOrUpdateQueryParam,
  deleteQueryParam,
  clearQueryParams,
  getQueryParam,
  getAllQueryParams,
  Params,
  serialize,
  addAuthCode,
};
